﻿using System;
using System.Data;
using System.Configuration;
using MySql.Data.MySqlClient;

namespace NDapper
{
    public interface IDapperContext : IDisposable
    {
        IDbConnection Connection { get; }
    }

    public class DapperContext : IDapperContext
    {
        private readonly string _connectionStringName;
        private readonly string _connectionString;
        private bool _useMiniProfiler;
        private IDbConnection _connection;

        public DapperContext()
        {
            //var temp = ConfigurationManager.AppSettings["UseMiniProfilerForSql"];
            //if (!bool.TryParse(temp, out _useMiniProfiler))
            //{
            //    _useMiniProfiler = false;
            //}
            //_connectionStringName = ConfigurationManager.AppSettings["UsedConnectionString"];
            //_connectionString = ConfigurationManager.ConnectionStrings[_connectionStringName].ConnectionString; 

            _connectionString = "Server=127.0.0.1;Port=3306;Database=bbtest;Uid=root;Pwd=root;charset=utf8;pooling=true";
        }

        public IDbConnection Connection
        {
            get
            {
                if (_connection == null)
                {
                    //if (_useMiniProfiler)
                    //{
                    //    _connection = new StackExchange.Profiling.Data.ProfiledDbConnection(new SqlConnection(_connectionString), MiniProfiler.Current);
                    //}
                    //else
                    //{
                        _connection = new MySqlConnection(_connectionString);
                    //}
                }
                if (_connection.State != ConnectionState.Open)
                {
                    _connection.Open();
                }
                return _connection;
            }
        }

        public void Dispose()
        {
            if (_connection != null && _connection.State == ConnectionState.Open)
            {
                _connection.Close();
            }
        }
    }
}